package light.mvc.controller.doctor;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.base.Json;
import light.mvc.page.base.Status;
import light.mvc.page.doctor.MedicinePrescription;
import light.mvc.page.doctor.Prescription;
import light.mvc.service.doctor.RegisterMedicineServiceI;
import light.mvc.service.patient.RegisterSeqServiceI;
import light.mvc.utils.TimeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping("/registerMedicine")
public class RegisterMedicineController extends BaseController {
	@Autowired
	private RegisterMedicineServiceI registerMeidicineSevice;
	
	@Autowired
	private RegisterSeqServiceI registerSeqService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		Date timeSegment = null;
		try {
			timeSegment = TimeUtil.getNowTimeSegment();
		} catch (Exception e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("errorCode", "1");
			return "/doctor/registerMedicine";
		}
		request.setAttribute("msg", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timeSegment));
		request.setAttribute("errorCode", "0");
		return "/doctor/registerMedicine";
	}
	
	@RequestMapping("/detailPage")
	public String detailPage(HttpServletRequest request,Integer registerId) {
		Prescription prescription = registerMeidicineSevice.getPrescription(registerId);
		
		List<MedicinePrescription> medicinePrescriptions = prescription.getMedicinePrescriptions();
		Date medicineTime = prescription.getMedicineTime();
		Date addTime = prescription.getAddTime();
		Date chargeTime = prescription.getChargeTime();
		
		request.setAttribute("addTime", addTime);
		request.setAttribute("chargeTime", chargeTime);
		request.setAttribute("medicineTime", medicineTime);
		request.setAttribute("medicinePrescriptions", medicinePrescriptions);
		request.setAttribute("registerId", registerId);
		return "/doctor/registerMedicineDetail";
	}
	
	@RequestMapping("/historyPage")
	public String historyPage(HttpServletRequest request,Integer patientId,Date timeSegment) {
		List<MedicinePrescription> medicinePrescriptions = registerMeidicineSevice.getPrescriptionByPatientId(patientId,timeSegment);
		request.setAttribute("medicinePrescriptions", medicinePrescriptions);
		return "/doctor/registerMedicineHistory";
	}
	
	@RequestMapping("/addPrescription")
	@ResponseBody
	public Json addPrescription(Integer registerId,String medicinePrescriptionStr) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		medicinePrescriptionStr = medicinePrescriptionStr.replace("&quot;", "\"");

		List<MedicinePrescription> medicinePrescriptions = JSONArray.parseArray(medicinePrescriptionStr, MedicinePrescription.class);
		
		try {
			registerMeidicineSevice.addPrescription(registerId, medicinePrescriptions);
			errorCode = 0;
			errorMsg = "开单成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		
		j = new Json(new Status(errorCode, errorMsg),null);
		return j;
	}
	
	@RequestMapping("/deletePrescription")
	@ResponseBody
	public Json deletePrescription(HttpServletRequest request,Integer registerId) {
		Json j = null;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			registerMeidicineSevice.deletePrescription(registerId);
			errorMsg = "删除旧药单成功";
			errorCode = 0;
			j = new Json(new Status(errorCode, errorMsg),null);
		} catch (Exception e) {
			errorMsg = e.getMessage();
			j = new Json(new Status(errorCode, errorMsg),null);
		}
		
		return j;
	}
	
	@RequestMapping("/charge")
	@ResponseBody
	public Json charge(Integer registerId) {
		Json j;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			registerMeidicineSevice.charge(registerId);
			errorCode = 0;
			errorMsg = "缴费成功";
		} catch (Exception e) {
			errorMsg = e.getMessage();
		}
		
		j = new Json(new Status(errorCode, errorMsg),null);
		return j;
	}
	
	@RequestMapping("/takeMedicine")
	@ResponseBody
	public Json takeMedicine(HttpServletRequest request,Integer registerId) {
		Json j = null;
		String errorMsg = null;
		Integer errorCode = -1;
		
		try {
			registerMeidicineSevice.takeMedicine(registerId);
			errorMsg = "取药成功";
			errorCode = 0;
			j = new Json(new Status(errorCode, errorMsg),null);
		} catch (Exception e) {
			errorMsg = e.getMessage();
			j = new Json(new Status(errorCode, errorMsg),null);
		}
		
		return j;
	}
	
	

}
